# -*- coding: iso-8859-2 -*-

import re

from authorshipanalyzer.authorslot import AuthorSlot
from authorshipanalyzer.slot.features import Age
from authorshipanalyzer.slot.features import Status

class Selector:
    '''
    Class selecting author slots according to the single slot of unknown author
    '''
    
    def select(self, unknownAs, knownAs):
        '''
        @unknownAs: AuthorSlot of unknown authorship
        @knownAs: list of AuthorSlots of known authorship
        '''
        dismissedAuthors = []
        
        unknown = unknownAs
        for known in knownAs:
            
            # Diacritics usage selector
            if unknown.useDiacritics != known.useDiacritics:
                dismissedAuthors.append(known.author)
                print "%-35s dismissed because of diacrtics mismatch" % known.author
                continue
            
            # Sex selector
            if unknown.sex != "unknown":
                if unknown.sex != known.sex and known.sex != "unknown":
                    dismissedAuthors.append(known.author)
                    print "%-35s dismissed because of sex mismatch" % known.author
                    continue
            
            # Age selector
            if unknown.age != [] and known.age != []:
                for uage in unknown.age:
                    umatch = self.getAge(uage, "ABOVE")
                    if umatch:
                       for kage in known.age:
                           kmatch = self.getAge(kage, "BELOW")
                           if kmatch:
                               if umatch > kmatch:
                                   dismissedAuthors.append(known.author)
                                   print "%-35s dismissed because of age mismatch" % known.author
                                   break
                               
                    umatch = self.getAge(uage, "BELOW")
                    if umatch:
                       for kage in known.age:
                           kmatch = self.getAge(kage, "ABOVE")
                           if kmatch:
                               if umatch < kmatch:
                                   dismissedAuthors.append(known.author)
                                   print "%-35s dismissed because of age mismatch" % known.author
                                   break
            
            if known.author in dismissedAuthors:
                continue
            
            if unknown.status != [] and known.status != []:
                for unkStatus in unknown.status:
                    # if unknown if student and known is not
                    if unkStatus == Status.STUDENT and Status.STUDENT not in known.status:
                        dismissedAuthors.append(known.author)
                        print "%-35s dismissed because of status mismatch" % known.author
                        break
                    
                    # if unknown is  not a student and known is
                    if unkStatus != Status.STUDENT and Status.STUDENT in known.status:
                        dismissedAuthors.append(known.author)
                        print "%-35s dismissed because of status mismatch" % known.author
                        break
                    
                    unkDegree = None
                    knDegree = None
                    degreeTitles = [ Status.DOCTOR, Status.MASTER_OF_SCIENCE, Status.PROFFESSOR ]
                    for degreeTitle in degreeTitles:
                        if degreeTitle in unknown.status:
                            unkDegree = degreeTitle
                        if degreeTitle in known.status:
                            kDegree = degreeTitle
                            
                    if unkDegree != None and knDegree != None and unkDegree != knDegree:
                        dissmisedAuthors.append(known.author)
                        print "%-35s dismissed because of status mismatch" % known.author
                        break
            
        return dismissedAuthors
    
    def getAge(self, tag, label):
        items = re.split("_", tag)
        if len(items) == 2:
            if items[0] == label:
                return int(items[1])
        return None
            
            